#!/usr/bin/python3
# -*- coding: utf-8 -*-
'模塊注釋'
from time import sleep

__author__ = 'Grant Wu'
import eventlet


def handle(index, queue: eventlet.Queue):
    print('======>begin handle')
    for x in range(10):
        print('index - %s : %s' % (index, x))
        eventlet.sleep(0.2)
    result = 'sucess!! %s' % index
    queue.put(item = result,block=True)
    print('======>result: %s' % result)
    return result

def subWorker():
    result = eventlet.spawn(handle, 1)
    result2 = eventlet.spawn(handle, 2)
    wait = result.wait()
    wait2 = result2.wait()
    print('result1: %s' % wait)
    print('result12: %s' % wait2)
    print('finishe!!')

def workerPool():
    eventlet.monkey_patch(socket = True, select = True)
    greenpool= eventlet.GreenPool(3)
    for i in range(10):
        result = greenpool.spawn(handle, i)
        # print('======>result: %s' % result)
    greenpool.waitall()

def workerPoolWithFuture():
    # 开启线程池，获取执行结果
    queue = eventlet.Queue()
    # 猴子补丁
    eventlet.monkey_patch(socket = True, select = True)
    greenpool= eventlet.GreenPool(3)
    for i in range(10):
        result = greenpool.spawn_n(handle, i, queue)
    # 线程池等待
    greenpool.waitall()
    # 从队列获取执行结果
    while not queue.empty():
        msg = queue.get()
        print('======>get msg from queue:%s' % msg)


if __name__ == '__main__':
    # workerPool()
    # subWorker()
    workerPoolWithFuture()


